package com.zdgk.core.common.utils;


import cn.hutool.core.io.checksum.crc16.CRC16Modbus;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;

/**
 * 进制工具类
 */
public class RadixUtils {

    /**
     * 十六进制字符串转字节数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr(String hexString) {
        if (StrUtil.isEmpty(hexString)) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        int index = 0;
        byte[] bytes = new byte[len / 2];
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
            index += 2;
        }
        return bytes;
    }

    /**
     * 字节数组转十六进制字符串
     *
     * @param bytes
     * @return
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        result = result.substring(0, result.length() - 1);
        return result;
    }

    /**
     * 十六进制字符串转为字符串集合
     *
     * @param hexData
     * @return
     */
    public static ArrayList<String> toStringList(String hexData) {
        ArrayList<String> arrayList = new ArrayList<>();
        if (hexData == null || hexData.equals("")) {
            return null;
        }
        hexData = hexData.replaceAll(" ", "");
        int len = hexData.length();
        if (len % 2 != 0) {
            return null;
        }
        int num = 0;
        while (num < len) {
            String s = hexData.substring(num, num + 2);
            arrayList.add(s);
            num = num + 2;
        }
        return arrayList;
    }

    /**
     * 十六进制字符串转为十进制整形集合
     *
     * @param hexData
     * @return
     */
    public static ArrayList<Integer> toIntegerList(String hexData) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        if (hexData == null || hexData.equals("")) {
            return null;
        }
        hexData = hexData.replaceAll(" ", "");
        int len = hexData.length();
        if (len % 2 != 0) {
            return null;
        }
        int num = 0;
        while (num < len) {
            String s = hexData.substring(num, num + 2);
            arrayList.add(Integer.parseInt(s, 16));
            num = num + 2;
        }
        return arrayList;
    }

    /**
     * 十进制转16进制字符串 * 字符串不足4位前面补0 *
     *
     * @param num
     * @return
     */
    public static String integerToHex(Integer num) {
        String readStartAddressHex = Integer.toHexString(num);
        String replace = String.format("%4s", readStartAddressHex).replace(" ", "0");
        StringBuffer stringBuilder1 = new StringBuffer(replace);
        stringBuilder1.insert(2, " ");
        return stringBuilder1.toString();
    }

    /**
     * CRC16Modbus 返回小小端序16进制字符串
     *
     * @param bytes
     * @return
     */
    public static String cRC16ModbusStr(byte[] bytes) {
        CRC16Modbus crc16Modbus = new CRC16Modbus();
        // 计算CRC-16校验值（使用默认的初始值和多项式）
        crc16Modbus.update(bytes, 0, bytes.length);
        // 获取校验值
        long crc16Value = crc16Modbus.getValue();
        String hex = Long.toHexString(crc16Value);
        hex = String.format("%4s", hex).replace(" ", "0");

        String h = hex.substring(0, 2);
        String l = hex.substring(2, 4);
        return l + " " + h;
    }

}
